package zero.tech.games.log.queue;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import com.alibaba.fastjson.JSONObject;

import zero.tech.games.common.QueueConsts;
import zero.tech.games.common.ThreadRunner;
import zero.tech.games.entity.log.LogGoldChange;
import zero.tech.games.log.hot.LogGoldChangeUtil;
import zero.tech.games.log.shareding.DynamicCreateTableSharding;
import zero.tech.redis.queue.QueueUtil;
@Configuration
@AutoConfigureAfter(DynamicCreateTableSharding.class)
public class LogGoldChangeProcess extends ThreadRunner{
	private static final Logger logger=LoggerFactory.getLogger(LogGoldChangeProcess.class);
	@Autowired
	private QueueUtil queueUtil;
	@Autowired
	private LogGoldChangeUtil logGoldChangeUtil;
	@Override
	public void process() {
		
		while(true) {
			int sleep = 0;
			List<String> list = queueUtil.pullQueueBatch(QueueConsts.QUEUE_LOG_GOLD_CHANGE, 100);
			try {
				if (list == null || list.isEmpty()) {
					sleep = 1000;
				} else {
					// 开始处理业务
					long time = System.currentTimeMillis();
					logger.info("============================开始茶叶账户变化日志入库==============================");
					
					List<LogGoldChange> logGoldChanges=new ArrayList<LogGoldChange>();
					for(String str:list) {
						LogGoldChange logGoldChange = JSONObject.parseObject(str,LogGoldChange.class);
						logGoldChanges.add(logGoldChange);
					}
					if(!logGoldChanges.isEmpty()) {
						logGoldChangeUtil.insertBatch(logGoldChanges);
					}
					logger.info("=====================结束茶叶账户变化日志入库,操作耗时:{}毫秒========================",
							(System.currentTimeMillis() - time));
				}
			} catch (Exception e) {
				e.printStackTrace();
				sleep = 1000;
			}
			if (sleep != 0) {
				try {
					Thread.sleep(sleep);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}

}
